package com.chance.cc.crawler.prod.command.job.domain.news.bilibili.trace;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.prod.command.job.domain.news.bilibili.BiliBiliCommonScript;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-10 12:06:33
 * @email okprog@sina.com
 */
public class BiliBiliTraceCrawlerSchedulerJob extends BiliBiliCommonScript {
    private static final String crawler_level = "trace";
    private static final String siteBiz = "video-trace";

    public static void main(String[] args) {
        publishCrawlerSchedulerJobInfo();
//        System.out.println(crawlerScheduler().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = crawlerScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("bilibili视频24h追溯采集任务");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServiceIp,metaServicePort);
        HttpPage httpPage = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob crawlerScheduler(){

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .recordKey("https://www.bilibili.com/")
                .httpUrl("https://www.bilibili.com/")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .domain(domain)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-",domain,siteBiz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .needParsed(false)
                .needWashed(false)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        //添加评论去重信息
        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,siteBiz,"comment_queue")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",domain,crawler_level, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, crawler_level, "queue")))
                .kafkaResultPipeline(null,kafkaTopicForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .supportRecord(getKwRecord(kwSite))
                .build();

        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());
        return crawlerJob;
    }
}
